package com.t2t.top.framework.rocketmq.client.extension;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;

public enum SerializeType {
    FASTJSON, JAVA, SerializeUtil;

    public byte[] serialize(Serializable messageObject) {
        byte[] jsonBytes = null;
        switch (this) {
            case FASTJSON:
                jsonBytes = JSON.toJSONBytes(messageObject, SerializerFeature.WriteClassName,
                        SerializerFeature.BrowserCompatible);
                break;
            case JAVA:
                jsonBytes = SerializeUtil.serialize(messageObject);
                break;
            default:
                throw new UnsupportedOperationException("serializeType is not supported. ");
        }
        return jsonBytes;
    }

    public Object deserialize(byte[] messageBody) {
        Object obj = null;
        switch (this) {
            case FASTJSON:
                obj = JSON.parseObject(messageBody, Object.class);
                break;
            case JAVA:
                obj = SerializeUtil.deserialize(messageBody);
                break;
            default:
                throw new UnsupportedOperationException("serializeType is not supported. ");
        }
        return obj;
    }
}